﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using Telerik.Charting;
using System.Drawing;
using Telerik.WinControls.UI;
using Layers;
using HoldemGame;
using System.Windows.Forms;

namespace HoldemView
{
    public class TelerikChartAdapter 
    {
        private ChartSeries _series;
        private IChartData _data;
        private ChartForm _form;
        private Timer _timer;

        public TelerikChartAdapter(IChartData data, ChartForm form, ChartSeries series)
        {
            _form = form;
            _data = data;
            _data.Changed += new NotifyCollectionChangedEventHandler(Data_Changed);
            _series = series;

            Update(0, _data.Count-1);
        }


        public TelerikChartAdapter(IChartData data, ChartForm form)
            : this(data, form, new ChartSeries(data.ID, ChartSeriesType.Line))
        {
        }

        void Data_Changed(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == NotifyCollectionChangedAction.Add)
            {
                for (int i = 0; i < e.NewItems.Count; i++)
                {
                    ChartSeriesItem item = new ChartSeriesItem();
                    SetItem(item, e.NewItems[i], _series.Items.Count);
                    _series.Items.Add(item);
                }
            }
            else if (e.Action == NotifyCollectionChangedAction.Reset)
            {
                _series.Items.Clear();
            }
            _form.ChartDirty = true;
        }

        private void Update(int start, int last)
        {
            for (int i = start; i <= last; i++)
                if (i >= _series.Items.Count)
                    while (_series.Items.Count <= i)
                    {
                        ChartSeriesItem item = new ChartSeriesItem();
                        _series.Appearance.ShowLabels = false;
                        
                        SetItem(item, _data[i], _series.Items.Count);
                        _series.Items.Add(item);
                        
                    }
                else
                {
                    ChartSeriesItem item = _series[i];
                    SetItem(item, _data[i], i);
                }
            //_form.Chart.Update();
           // _series.Parent.Parent.SetDirty();
            //_form.Chart.UpdateGraphics();
            //_form.Chart.Refresh();
        }
        
        protected virtual void SetItem(ChartSeriesItem item, object value, double x)
        {
            if (value is double)
            {
                item.YValue = (double)value;
                item.XValue = x;
            }
            else if (value is XY)
            {
                XY xy = (XY)value;
                item.XValue = xy.X;
                item.YValue = xy.Y;
            }
        }
    }

    public class TelerikChartManager
    {
        private Dictionary<string, ChartForm> _charts = new Dictionary<string, ChartForm>();
        private Dictionary<IChartData, TelerikChartAdapter> _adapters = new Dictionary<IChartData, TelerikChartAdapter>();

        private static TelerikChartManager _Instance;

        public static TelerikChartManager Instance
        {
            get { if (null == _Instance) new TelerikChartManager(); return _Instance; }
        }

        public TelerikChartManager()
        {
            _Instance = this;
        }

        private ChartForm NewChart(string chartId)
        {
            ChartForm cf = new ChartForm();
            _charts.Add(chartId, cf);
            return cf;

        }

        public ChartForm this[string chartId]
        {
            get 
            {
                ChartForm chart = null;
                _charts.TryGetValue(chartId, out chart);
                return chart;
            }
        }

        public TelerikChartAdapter Show(string chartId, IChartData data)
        {
            ChartForm chart = this[chartId];
            if (null == chart)
                chart = NewChart(chartId);
            ChartSeries series = new ChartSeries(data.ID, ChartSeriesType.Line);
            chart.Series.Add(series);
            TelerikChartAdapter adptr = new TelerikChartAdapter(data, chart, series);
            _adapters.Add(data, adptr);
            chart.Text = chartId;
            chart.Chart.ChartTitle.TextBlock.Text = chartId;
            if (!chart.Visible)
            {
                chart.Show();
                chart.Visible = true;
            }
         
            return adptr;
        }
    }
}